package com.zzuli.yxy.controller;

import com.zzuli.yxy.entity.User;
import com.zzuli.yxy.enumeration.AppHttpCodeEnum;
import com.zzuli.yxy.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zzuli.yxy.service.UserService;

import java.util.List;

/**
 * @auther pony
 * @create 2023-05-16 18:30
 */
@RestController
@RequestMapping("user")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 获取用户管理分页信息
     * @param pageCurrent 当前页
     * @param pageSize 每页条数
     * @param name 关键字 非必传
     * @return
     */
    @GetMapping({"/page/{pageCurrent}/{pageSize}",
            "/page/{pageCurrent}/{pageSize}/{name}"})
    public R getPage(@PathVariable("pageCurrent") Integer pageCurrent,
                     @PathVariable("pageSize") Integer pageSize,
                     @PathVariable(name = "name", required = false) String name){
        return userService.getPage(pageCurrent, pageSize, name);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public R addSinger(@RequestBody User user) {

        // 数据非空判断，主要是 歌曲名和状态
        if (user.getNickName()==null||"".equals(user.getNickName())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return userService.addUser(user);
    }

    /**
     * 添加用户-上传用户图片
     * @param file 图片
     * @param userId 要添加图片的用户id
     * @return
     */
    @PostMapping("/uploadAddImg")
    public R uploadAddImg(MultipartFile file, @RequestParam String userId) {
        // 1、文件校验
        // 是否是图片文件 image/jpeg，是不是以 image 开头的
        if (!file.getContentType().startsWith("image")){
            return R.error(AppHttpCodeEnum.IMAGE_ERROR_TYPE);
        }
        if (file.getSize() > 30*1024*1024){
            return R.error(AppHttpCodeEnum.FILE_EXCEED);
        }
        // 2、歌手id 非空检查
        if ("".equals(userId)||userId==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return userService.uploadAddImg(file, userId);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteSingerById(@PathVariable("id") Integer id) {
        return userService.deleteById(id);
    }

    /**
     * 根据用户id，查询单个用户信息
     * @param id 用户id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return userService.selectOneById(id);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public R updateSinger(@RequestBody User user){
        //非空判断
        if ("".equals(user.getNickName())||user.getNickName()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return userService.updateUser(user);
    }

    /**
     * 批量删除
     * @param ids  id数组
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        if(ids == null || ids.size() ==0 ){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return userService.deleteBatch(ids);
    }
}
